package com.unnous.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则校验工具类
 * author unnous
 * 2018-09-25
 */
public class RegexUtil {

    /**
     * 邮箱验证
     *
     * @param mail 邮件地址
     * @return 返回校验结果 真为true 否为false
     */
    public static boolean mailCheck(String mail) {
        try {
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(mail);
            return matcher.matches();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 手机号验证
     *
     * @param phone 手机号
     * @return 返回校验结果 真为true 否为false
     */
    public boolean phoneCheck(String phone) {
        boolean flag = false;
        try {
            Pattern regex = Pattern.compile("^1[345789]\\d{9}$");
            Matcher matcher = regex.matcher(phone);
            return matcher.matches();
        } catch (Exception e) {
            return false;
        }
    }
}
